﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.MODEL.FoundationModel;
using SmallERP.DAL;
using System.Data;

namespace SmallERP.Bll.BaseInfo
{
    public class PrinterSet
    {


        /// <summary>
        /// 获得打印机的ICC路径及锐化值
        /// </summary>
        /// <param name="printerId"></param>
        /// <param name="paperTypeName"></param>
        /// <returns></returns>
        public PrinterSeting_info GetPrinterIccPath(int printerId, string paperTypeName)
        {
            PrinterSeting_info printerSetInfo = new PrinterSeting_info();
            //using (smallerpDataContext entites = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PaperType paperType = entites.PaperType.FirstOrDefault(p => p.PaperTypeName == paperTypeName);
            //        PrinterSeting printerSet = entites.PrinterSeting.FirstOrDefault(p => p.PrinterId == printerId && p.PaperTypeId == paperType.PaperTypeId);
            //        printerSetInfo.ICCPATH = printerSet.ICCPATH;
            //        printerSetInfo.PaperTypeId = printerSet.PaperTypeId;
            //        printerSetInfo.PrinterId = printerSet.PrinterId;
            //        printerSetInfo.Sharpening = printerSet.Sharpening;
            //    }
            //    catch
            //    {
            //        printerSetInfo = null;
            //    }
            //}

            //string papertypeSql = "select PaperTypeId from PaperType where PaperTypeName " + paperTypeName;
            //DataTable typeTable=DAL.AccessHelper.dataTable(papertypeSql);
            // int paperTypeID=0;
            // if (typeTable.Rows.Count > 0)
            // {
            //     paperTypeID = Convert.ToInt32(typeTable.Rows[0][0]);
            // }
            // else
            // {

            // }
            string sql = "select ICCPATH,PaperTypeId,PrinterId,Sharpening from PaperType,PrinterSeting where PaperType.PaperTypeId=PrinterSeting.PaperTypeId and PaperType.PaperTypeName='" + paperTypeName + "' and PrinterSeting.PrinterId=" + printerId+"'";
            DataTable Table = DAL.AccessHelper.dataTable(sql);
            if (Table.Rows.Count > 0)
            {
                printerSetInfo.ICCPATH = Table.Rows[0]["ICCPATH"].ToString();
                printerSetInfo.PaperTypeId = Convert.ToInt32(Table.Rows[0]["PaperTypeId"]);
                printerSetInfo.PrinterId = Convert.ToInt32(Table.Rows[0]["PrinterId"]);
                printerSetInfo.Sharpening = Convert.ToInt32(Table.Rows[0]["Sharpening"]);
                return printerSetInfo;
            }
            else
            {
                return null;
            }


        }

        public int Insert(PrinterSeting_info printerSetInfo)
        {
            int ret = 0;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PrinterSeting printerSet = new PrinterSeting();
            //        printerSet.PrinterId = printerSetInfo.PrinterId;
            //        printerSet.printer = entities.printer.FirstOrDefault(p => p.PrinterID == printerSetInfo.PrinterId);
            //        printerSet.PaperTypeId = printerSetInfo.PaperTypeId;
            //        printerSet.PaperType = entities.PaperType.FirstOrDefault(p => p.PaperTypeId == printerSetInfo.PaperTypeId);
            //        printerSet.ICCPATH = printerSetInfo.ICCPATH;
            //        printerSet.Sharpening = printerSetInfo.Sharpening;
            //        entities.AddToPrinterSeting(printerSet);
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}

            string sql = "select ICCPATH,PaperTypeId,PrinterId,Sharpening from PrinterSeting where PrinterSeting.PaperTypeId='" + printerSetInfo.PaperTypeId + "' and PrinterSeting.PrinterId='" + printerSetInfo.PaperTypeId+"'";
            DataTable Table = DAL.AccessHelper.dataTable(sql);
            string dota= " , ";
            if (Table.Rows.Count == 0)
            {
                string Sql = "insert into PrinterSeting(PrinterId,PaperTypeId,Sharpening,ICCPATH) values('" + printerSetInfo.PrinterId + dota + printerSetInfo.PaperTypeId + dota + printerSetInfo.Sharpening + dota + printerSetInfo.ICCPATH + "')";
                ret = DAL.AccessHelper.ExecuteNonQuery(Sql);
            }
            else
            {
                ret = -2;
            }

            return ret;

        }
        /// <summary>
        /// 设置打印机的ICC路径及锐化值
        /// </summary>
        /// <param name="printerSetInfo"></param>
        /// <returns></returns>
        public int Update(PrinterSeting_info input)
        {
            int ret = 0;
            //using (smallerpDataContext entities = new smallerpDataContext())
            //{
            //    try
            //    {
            //        PrinterSeting printerSet = entities.PrinterSeting.FirstOrDefault(p => p.PrinterId == printerSetInfo.PrinterId && p.PaperTypeId == printerSetInfo.PaperTypeId);
            //        printerSet.ICCPATH = printerSetInfo.ICCPATH;
            //        printerSet.Sharpening = printerSetInfo.Sharpening;
            //        entities.SaveChanges();
            //    }
            //    catch
            //    {
            //        ret = -1;
            //    }
            //}
            string sql = "select ICCPATH,PaperTypeId,PrinterId,Sharpening from PrinterSeting where PrinterSeting.PaperTypeId='" + input.PaperTypeId + "' and PrinterSeting.PrinterId='" + input.PaperTypeId+"'";
            DataTable Table = DAL.AccessHelper.dataTable(sql);
            string dota=" , ";
            if (Table.Rows.Count == 0)
            {
                ret = -2;
            }
            else
            {
                string updateSql = "update PrinterSeting set ICCPATH='" + input.ICCPATH + "'," + " Sharpening='" + input.Sharpening + "' where PrinterSeting.PaperTypeId='" + input.PaperTypeId + "' and PrinterSeting.PrinterId='" + input.PaperTypeId+"'";
                ret = DAL.AccessHelper.ExecuteNonQuery(updateSql);
            }
            return ret;
        }
    }
}
